package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.RoomDao;
import com.kun.portal.webframe.dao.SignInDao;
import com.kun.portal.webframe.po.FlightInfoPo;
import com.kun.portal.webframe.po.FlightPlanePo;
import com.kun.portal.webframe.po.RoomPo;
import com.kun.portal.webframe.po.SignInPo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;

/**
 * Created by Administrator on 2018/5/10.
 */
@Service
public class SignInService  {
    @Autowired
    private SignInDao signInDao;
    public SignInPo save(SignInPo signInPo){
        return signInDao.save(signInPo);
    }
    public Page<SignInPo> query(Pageable page,Date StartDate,Date endDate,String flightNum){
        return signInDao.findAll(page);
    }
    public Page<SignInPo> queryService(Pageable pageable ,String roomId, String flightInfoId){
        Specification query =  new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Predicate predicate = null;
                if(StringUtils.isNotBlank(roomId)){
                     RoomPo room= new RoomPo();
                     room.setRoomId(roomId);
                     predicate = cb.equal(root.get("room"),room);
                }
                if(StringUtils.isNotBlank(flightInfoId)){
                    Predicate predicate1 =  cb.equal(root.get("flightPlane").get("flightInfo").get("flightInfoId"),flightInfoId);
                    if(predicate==null){
                        predicate=predicate1;
                    }else{
                        predicate = cb.and(predicate,predicate1);
                    }
                }
                return predicate;
            }
        };
        return signInDao.findAll(query,pageable);
    }
}
